package com.dongdongshop.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


//IOC容器
@Component
@Aspect//声明是一个切面类 OOP延续 面向切面编程 封装 继承 多态
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.log.DKLog)")
    public void aopLog(){

    }

    //AOP 通知 前置通知
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        logger.info("进入方法: {} , 参数为 : {} ",signature,args);
    }

    //返回后通知
    @AfterReturning(pointcut = "aopLog()",returning = "obj")
    public void  doAfterReturning(Object obj){
        //打印返回值
        logger.info("方法执行成功, 返回值为 : {}", JSONObject.toJSONString(obj));
    }
}
